<?php

if (function_exists('add_theme_support')) {

    add_theme_support('post-thumbnails');

}

define(TH_TEMPLATE_URL, get_bloginfo('template_url'));

function my_scripts_method() {



// register your script location, dependencies and version
        wp_deregister_script('jquery-ui-core'); //deregistro il jquery ui di WP lo includo nell'header
        wp_register_script('jqueryui', TH_TEMPLATE_URL . '/js/jquery.ui.js', array(), 1.0, TRUE);
        wp_register_script('jquerycolors', TH_TEMPLATE_URL . '/js/jquery.color.js', array(), 1.0, FALSE);
        wp_register_script('uiwheel', TH_TEMPLATE_URL . '/js/fancybox/lib/jquery.mousewheel-3.0.6.pack.js', array(), 1.0, TRUE);
        wp_register_script('extlidisc', TH_TEMPLATE_URL . '/js/externallinkdisclaimer.js', array(), 1.0, TRUE);
        wp_register_script('gestSottomenu', TH_TEMPLATE_URL.'/js/gestSottomenu.js',array(),1.0,TRUE);
        wp_register_script('fixbar', TH_TEMPLATE_URL . '/js/fixbar.js',  array(),1.0,TRUE);
        
        
        //::::::::::::::::::::::::::: PAGINA DOMANDE E RISPOSTE :::::::::::::::::::::::::::::::::://
        wp_register_script('domanderisposte', TH_TEMPLATE_URL . '/js/domanderisposte.js', array(), 1.0, TRUE);
        wp_register_script('formscript', TH_TEMPLATE_URL . '/js/formscript.js', array(), 1.0, TRUE);
        //::::::::::::::::::::::::::: PAGINA DOMANDE E RISPOSTE :::::::::::::::::::::::::::::::::://
        
        //::::::::::::::::::::::::::: PAGINA INCONTINENZA URINARIA ::::::::::::::::::::::::::::::://
        wp_register_script('incontinenzaurinaria', TH_TEMPLATE_URL . '/js/incontinenzaurinaria.js', array(), 1.0, TRUE);
        //::::::::::::::::::::::::::: PAGINA INCONTINENZA URINARIA :::::::::::::::::::::::::::::::::://
        
        //::::::::::::::::::::::::::: PAGINA LA SOLUZIONE ::::::::::::::::::::::::::::::://
        wp_register_script('laSoluzione', TH_TEMPLATE_URL . '/js/LaSoluzione.js',  array(),1.0,TRUE);
        //::::::::::::::::::::::::::: PAGINA LA SOLUZIONE ::::::::::::::::::::::::::::::://

    wp_enqueue_script("formscript");
    wp_enqueue_script('uiwheel');
    wp_enqueue_script('extlidisc');
    wp_enqueue_script('jqueryui');
    wp_enqueue_script('jquerycolors');
    
    if(!is_page("home"))
      wp_enqueue_script('fixbar');
    
    if(is_page('incontinenza-urinaria')||is_page('la-soluzione-lines-perla')|| is_page('informazioni-utili')||is_page('professionisti')){
        wp_enqueue_script('gestSottomenu');
    }
    if (is_page('domande-e-risposte') || is_page('incontinenza-urinaria') || is_page('la-soluzione-lines-perla')){
      wp_enqueue_script('domanderisposte');
    }
    if (is_page('incontinenza-urinaria')){
      wp_enqueue_script('incontinenzaurinaria');
    }
    if(is_page('la-soluzione-lines-perla')){
        wp_enqueue_script('laSoluzione');
    }
    
}

    
require_once "funzioniAjax.php";
add_action("wp_ajax_nopriv_adduser","aggiungiProfessionista"); 
add_action("wp_ajax_adduser","aggiungiProfessionista"); 
add_action("wp_ajax_invioDomandaProf","inviaDomandaProf"); 
//add_action("wp_ajax_nopriv_invioDomandaProf","inviaDomandaProf"); 
add_action("wp_ajax_accesso","lProf");
add_action("wp_ajax_nopriv_accesso","lProf");
add_action("wp_ajax_nopriv_askForTester","askForTester"); 
add_action("wp_ajax_askForTester","askForTester"); 

add_action("wp_ajax_nopriv_askForTesterAdmin","askForTesterAdmin"); 

add_action("wp_ajax_testerSent","testerSent");     

add_action('wp_enqueue_scripts', 'my_scripts_method');


/*
 * Resize images dynamically using wp built in functions
 * Victor Teixeira
 *
 * php 5.2+
 *
 * Exemplo de uso:
 * 
 * <?php 
 * $thumb = get_post_thumbnail_id(); 
 * $image = vt_resize( $thumb, '', 140, 110, true );
 * ?>
 * <img src="<?php echo $image[url]; ?>" width="<?php echo $image[width]; ?>" height="<?php echo $image[height]; ?>" />
 *
 * @param int $attach_id
 * @param string $img_url
 * @param int $width
 * @param int $height
 * @param bool $crop
 * @return array
 */
function vt_resize($attach_id = null, $img_url = null, $width, $height, $crop = false) {
    // this is an attachment, so we have the ID
    if ($attach_id) {
        $image_src = wp_get_attachment_image_src($attach_id, 'full');
        $file_path = get_attached_file($attach_id);
        // this is not an attachment, let's use the image url
    } else if ($img_url) {
        $file_path = parse_url($img_url);
        $file_path = $_SERVER['DOCUMENT_ROOT'] . $file_path['path'];
        //$file_path = ltrim( $file_path['path'], '/' );
        //$file_path = rtrim( ABSPATH, '/' ).$file_path['path'];
        $orig_size = getimagesize($file_path);
        $image_src[0] = $img_url;
        $image_src[1] = $orig_size[0];
        $image_src[2] = $orig_size[1];
    }
    $file_info = pathinfo($file_path);

    $extension = '.' . $file_info['extension'];
    // the image path without the extension

    $no_ext_path = $file_info['dirname'] . '/' . $file_info['filename'];
    $cropped_img_path = $no_ext_path . '-' . $width . 'x' . $height . $extension;
    // checking if the file size is larger than the target size

    // if it is smaller or the same size, stop right here and return

    if ($image_src[1] > $width || $image_src[2] > $height) {
        // the file is larger, check if the resized version already exists (for $crop = true but will also work for $crop = false if the sizes match)

        if (file_exists($cropped_img_path)) {
            $cropped_img_url = str_replace(basename($image_src[0]), basename($cropped_img_path), $image_src[0]);
            $vt_image = array(

                'url' => $cropped_img_url,

                'width' => $width,

                'height' => $height

            );
            return $vt_image;

        }
        // $crop = false

        if ($crop == false) {
            // calculate the size proportionaly

            $proportional_size = wp_constrain_dimensions($image_src[1], $image_src[2], $width, $height);

            $resized_img_path = $no_ext_path . '-' . $proportional_size[0] . 'x' . $proportional_size[1] . $extension;
            // checking if the file already exists

            if (file_exists($resized_img_path)) {
                $resized_img_url = str_replace(basename($image_src[0]), basename($resized_img_path), $image_src[0]);
                $vt_image = array(

                    'url' => $resized_img_url,

                    'width' => $proportional_size[0],

                    'height' => $proportional_size[1]

                );



                return $vt_image;

            }

        }
        // no cache files - let's finally resize it

        $new_img_path = image_resize($file_path, $width, $height, $crop);

        $new_img_size = getimagesize($new_img_path);

        $new_img = str_replace(basename($image_src[0]), basename($new_img_path), $image_src[0]);
        // resized output

        $vt_image = array(

            'url' => $new_img,

            'width' => $new_img_size[0],

            'height' => $new_img_size[1]

        );
        return $vt_image;

    }
    // default output - without resizing

    $vt_image = array(

        'url' => $image_src[0],

        'width' => $image_src[1],

        'height' => $image_src[2]

    );
    return $vt_image;

}
//funzione per stampare i vari slideshow
function slide($post_parent, $img_width, $img_height) {
    $args = array(

        'order' => 'ASC',

        'post_mime_type' => 'image',

        'post_parent' => $post_parent,

        'post_type' => 'attachment',

        'orderby' => 'menu_order ID',

    );
    $attachments = get_children($args);
    $res = '';
    foreach ($attachments as $a):
        $image = vt_resize('', $a->guid, $img_width, $img_height, true);
        $res .='<img src="' . $image[url] . '" width="' . $image[width] . '" height="' . $image[height] . '" alt="' . esc_html(get_post_meta($a->ID, '_wp_attachment_image_alt', true)) . '" title="' . esc_html($a->post_title) . '" />';
    endforeach;
    return $res;

}

/*
 *stampa o la excertp di un post o la lunghezza che gli viene passata come parametro 
 * 
 */
function excerpt(

$words = 50, $ptag = 'p', $link_text = '', $allowed_tags = '<i>,<em>,<b>,<strong>'

) {

    global $post;

    if ($allowed_tags == 'all')

        $allowed_tags = '<a>,<i>,<em>,<b>,<strong>,<ul>,<ol>,<li>,<span>,<blockquote>,<img>';

    $text = preg_replace('/\[.*\]/', '', strip_tags($post->post_content, $allowed_tags));

    $text = explode(' ', $text);

    $tot = count($text);

    for ($i = 0; $i < $words; $i++) : $output .= $text[$i] . ' ';

    endfor;

    if ($ptag == 'p')

        echo '<p>';

    echo force_balance_tags($output);

    if ($i < $tot)

        echo '...';

    if ($ptag == 'p')

        echo '</p>';

}

function voci_menu($nome_pag, $slug_pag,$wp_page, $desc) {

    $url = get_bloginfo('url') . '/';

    if (is_page($wp_page) || is_single($wp_page)):

        echo '<li id="' . $wp_page . '" class="attivo"><a class="barra_domande" href="#" title="Sei nella pagina &quot;' . $nome_pag . '&quot;">' . $nome_pag . '</a></li>';

    else: echo '<li id="' . $wp_page . '"><a class="barra_domande" href="' . $slug_pag . '" title="Vai alla pagina &quot;' . $nome_pag . '&quot; - ' . $desc . '">' . $nome_pag . '</a></li>';

    endif;

}
function chechTitle($indirizzo){
    $richiesta = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    if($indirizzo == $richiesta){
        return "Sei nella pagina";
    }else{
        return "Vai alla pagina";
    }
}
function classe($pagina){
     $richiesta = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
     if($richiesta == $pagina){
         return "attivo";
     }else{
         return '';
     }
}

function get_indirizzo($indirizzo){
       $richiesta = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
              
       return $richiesta==$indirizzo? "#": $indirizzo;
}

//INIZIO HACK BACKOFFICE

if (is_admin() || 20== get_current_user_id()):

    include ('hack.php');

endif;
add_action('admin_menu','wphidenag');

function wphidenag() {

remove_action( 'admin_notices', 'update_nag', 3 );

}
function fb_move_admin_bar() {

    echo '

    <style type="text/css">

    #wpadminbar {

       display: none;

    }

    

    </style>';

}

// remove the following line if you want to keep the admin bar at the top on the backend

add_action( 'admin_head', 'fb_move_admin_bar' );

// remove the following line if you want to keep the admin bar at the top on the frontend

add_action( 'wp_head', 'fb_move_admin_bar' );


//creazione del menu sul backend
add_action('admin_menu', 'perla_menu');
function perla_menu() {
    //Inizializza il menu per l'upload
    if(current_user_can('administrator')){
        add_menu_page('Domande in arrivo', "Domande in arrivo", "administrator", domande_in_arrivo, 'domande_in_arrivo');
        add_submenu_page(domande_in_arrivo,'Campioni inviati', "Campioni inviati", "administrator", campioni_inviati, 'campioni_inviati');
        add_submenu_page(domande_in_arrivo,'Campioni da inviare', "Campioni da inviare", "administrator", campioni_da_inviare, 'campioni_da_inviare');
        add_submenu_page(domande_in_arrivo,'Campioni per professionisti', "Campioni per professionisti", "administrator", campioni_professionisti, 'campioni_professionisti');
        }
}

function domande_in_arrivo(){
  require_once "domandeinarrivo.php";
  domandeinarrivo();
}

function campioni_inviati(){
  require_once "domandeinarrivo.php";
  campioninviati();
}

function campioni_da_inviare(){
  require_once "domandeinarrivo.php";
  campionidainviare();
}

function campioni_professionisti(){
  require_once "domandeinarrivo.php";
  campioniprofessionisti();
}



?>
